package com.example.datapedestal.dataAllocation.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.datapedestal.dataAllocation.controller.form.DbConfigListForm;
import com.example.datapedestal.dataAllocation.controller.form.PACsConfigListForm;
import com.example.datapedestal.dataAllocation.service.IDbConfigService;
import com.example.datapedestal.dataAllocation.mapper.DbConfigMapper;
import com.example.datapedestal.utils.EntityReflectionUtils;
import com.example.datapedestal.utils.JsonToMapExample;
import com.example.datapedestal.utils.vo.CommonResp;
import com.example.datapedestal.utils.vo.ResultCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.dataPedestal.dataAllocation.entity.DbConfig;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hcy
 * @since 2023-10-31
 */
@Service
public class DbConfigServiceImpl<T> extends ServiceImpl<DbConfigMapper, DbConfig> implements IDbConfigService<T> {
    @Autowired
    private  DbConfigMapper dbConfigMapper;

//    添加配置方法
    @Override
    public int addDbConfig(T ConfigListForm) {
        try {
            Map map = formatAnalysis(ConfigListForm);
            return dbConfigMapper.addDbConfig(map);
        } catch (Exception e) {
            return 0;
        }
    }

    //查询接口
    @Override
    public List<T> getConfig(int type) {
        QueryWrapper<DbConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        List<DbConfig> dbConfigs = dbConfigMapper.selectList(queryWrapper);
        List<T> transition;
        //数据库
        if(type==1){
            DbConfigListForm dbConfigListForm=new DbConfigListForm();
            transition = transition(dbConfigs, dbConfigListForm);
            //PACS
        }else{
            PACsConfigListForm paCsConfigListForm=new PACsConfigListForm();
            transition = transition(dbConfigs, paCsConfigListForm);
        }
        return transition;
    }

    //删除接口
    @Override
    public int deleteConfig(String configName) {
        QueryWrapper<DbConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("configName",configName);
        return dbConfigMapper.delete(queryWrapper);
    }

    //修改
    @Override
    public int updateDbConfig(String param,T ConfigListForm) {
        try {
            Map map = formatAnalysis(ConfigListForm);
            map.put("param",param);
            return dbConfigMapper.updateDbConfig(map);
        } catch (Exception e) {
            return 0;
        }
    }
    //把前端传的内容按固定格式封装
    public Map formatAnalysis(T ConfigListForm) throws Exception {
        //调用封装的方法获取指定属性名的内容
        int type = (int) EntityReflectionUtils.getField(ConfigListForm, "type");
        String configName = (String) EntityReflectionUtils.getField(ConfigListForm, "configName");
        // 创建 ObjectMapper 对象
        ObjectMapper objectMapper = new ObjectMapper();
        //把实体类转为字符串
        String json = objectMapper.writeValueAsString(ConfigListForm);
        Map map=new HashMap();
        map.put("configName",configName);
        map.put("json",json);
        map.put("type",type);
        return map;
    }
    //把查询结果转为List
    public List<T> transition(List<DbConfig> dbConfigs,Object obj){
        List dbConfigListVos=new ArrayList<>();
        dbConfigs.stream().forEach(item->{
            System.out.println("得到的数据内容 "+item);
            try {
                Object newObj = obj.getClass().newInstance();
                //把configuration的json配置信息转为map
                Map convert = JsonToMapExample.convert(item.getConfiguration());
                EntityReflectionUtils.setFieldValues(newObj,convert);
                EntityReflectionUtils.setField(newObj,"configName",item.getConfigName());
                EntityReflectionUtils.setField(newObj,"type",item.getType());
                dbConfigListVos.add(newObj);
                System.out.println("遍历内容 "+dbConfigListVos);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return  dbConfigListVos;
    }
}
